jQuery(document).ready(function (b) {
    b(".myo-poll-bar").bind("hover", function (a) {
        if (b(this).attr("data-myo-clicked") == "no") {
            b(this).css("cursor", "pointer")
        }
    });
    b(".myo-poll-holder-box").bind("hover", function (a) {
        if (b(this).attr("data-myo-clicked") == "no") {
            b(this).css("cursor", "pointer")
        }
    });
    b(".myo-vote-me").hide();
    b(".myo-poll-holder-box").bind("mouseenter", function (g) {
        var f = b(this).attr("data-myo-poll-id");
        var h = b(this).attr("data-myo-option");
        var a = b(this).children(".myo-poll-box").attr("data-myo-clicked");
        if (a == "no") {
            b(".myo-vote-me-" + f + "-" + h).show()
        }
    });
    b(".myo-poll-holder-box").bind("mouseleave", function (f) {
        var e = b(this).attr("data-myo-poll-id");
        var a = b(this).attr("data-myo-option");
        b(".myo-vote-me-" + e + "-" + a).hide()
    });
    b(".myo-poll-bar").bind("click", function (k) {
    	
    	var log =  window.loggedin;
    	var loginurl = window.loginurl;
    	var regurl = window.registerurl;

    	
    	if( log == 'no'){
    	
        b("body").css("overflow", "hidden");
		b("html").css("overflow", "hidden");
    	b('body').append('<div id = "myo-overlay"></div>');
    	b('#myo-overlay').append('<div id = "myo-container"></div>');
    	b('#myo-container').append('You must be logged in to vote. You can <a class = "myo-link" href = "'+ loginurl +'">Login</a> or '+ regurl +' to vote<br/>');
    	b('#myo-container').append('<br/><span style = "font-size:small;text-align:center">press esc to exit</span>');
    	offset = window.pageYOffset	
    	b("#myo-overlay").css({
			"top":        offset,
			"left":       "0"
		}).animate({"opacity":"1"}, 400, "linear");
        
    	return false
    	}
    	
    	
    	
        var i = b(this).attr("data-myo-poll-id");
        var m = b(this).attr("data-myo-option");
        var n = b(this).attr("data-myo-clicked");
        var a = b(this).attr("data-myo-perc");
        b(".myo-poll-" + i).css("cursor", "default");
        if (n == "yes") {
            return false
        }
        var j = {
            action: "myo_polling_vote",
            poll: i,
            option: m,
        };
        var l = b.ajax({
            url: MyAjax.ajaxurl,
            type: "POST",
            data: j,
            dataType: "json",
        });
        l.done(function (c) {
            var d = c.total;
            b(".myo-poll-" + i).each(function (f) {
                b("#myo-poll-" + i + "-" + f).attr("data-myo-clicked", "yes");
                b(".myo-poll-" + i).attr("data-myo-clicked", "yes");
                var e = Math.round(100 * c.votes[f] / d);
                b("#myo-poll-" + i + "-" + f).animate({
                    width: e + "%",
                }, 1500, function () {
                    if (a == "yes") {
                        b("#myo-poll-" + i + "-votes-" + f).append(" " + e + "%")
                    } else {
                        b("#myo-poll-" + i + "-" + f).html("");
                        b("#myo-poll-" + i + "-votes-" + f).html(c.option[f] + ": " + c.votes[f] + " votes")
                    }
                    b("#myo-poll-" + i + "-total").html("<b>" + c.total + "</b> votes")
                })
            })
        });
        l.fail(function (d, c) {
            alert("Request failed: " + c)
        });
        return true
    });
    
    
    b(".myo-link").bind("click", function(){
    	b("html").css("overflow", "visible");
    	b("body").css("overflow", "visible");
    });
    
    b(".myo-container").bind("click", function(){
    	removeOverlay2();
    });
    
  b(document).keyup(function(e) {

  if (e.keyCode == 27) {  	
  	removeOverlay2();
   } 
  });
  
  	function removeOverlay2() {
	// allow users to be able to close the lightbox
		b("body").css("overflow", "visible");
		b("html").css("overflow", "visible");
		b("#myo-overlay, #myo-container").remove();

	   return true;					
	}
    
    
    b(".myo-poll-holder-box").bind("click", function (k) {
    	
    	var log =  window.loggedin;
    	var loginurl = window.loginurl;
    	var regurl = window.registerurl;
    	
    	if( log == 'no'){
    	
        b("body").css("overflow", "hidden");
		b("html").css("overflow", "hidden");
    	b('body').append('<div id = "myo-overlay"></div>');
    	b('#myo-overlay').append('<div id = "myo-container"></div>');
    	b('#myo-container').append('You must be logged in to vote. You can <a class = "myo-link" href = "'+ loginurl +'">Login</a> or '+ regurl +' to vote<br/>');
    	b('#myo-container').append('<br/><span style = "font-size:small;text-align:center">press esc to exit</span>');
    	offset = window.pageYOffset	
    	b("#myo-overlay").css({
			"top":        offset,
			"left":       "0"
		}).animate({"opacity":"1"}, 400, "linear");
        
    	return false
    	}
    	
        b(this).css("cursor", "default");
        var i = b(this).children(".myo-poll-box").attr("data-myo-poll-id");
        var m = b(this).children(".myo-poll-box").attr("data-myo-option");
        var n = b(this).children(".myo-poll-box").attr("data-myo-clicked");
        var a = b(this).children(".myo-poll-box").attr("data-myo-perc");
        b(".myo-poll-holder-box-" + i).css("cursor", "default");
        b(this).children(".myo-vote-me-" + i + "-" + m).hide();
        if (n == "yes") {
            return false
        }
        var j = {
            action: "myo_polling_vote",
            poll: i,
            option: m,
        };
        var l = b.ajax({
            url: MyAjax.ajaxurl,
            type: "POST",
            data: j,
            dataType: "json",
        });
        l.done(function (c) {
            var d = c.total;
            b(".myo-poll-" + i).each(function (f) {
                var e = Math.round(100 * c.votes[f] / d);
                if (a == "yes") {
                    b("#myo-poll-" + i + "-box-" + f + "-votes").html(e + "%")
                } else {
                    b("#myo-poll-" + i + "-box-" + f + "-votes").html(c.votes[f]);
                    b("#myo-box-votes-" + i + "-" + f).html("votes")
                }
                b("#myo-poll-" + i + "-" + f).attr("data-myo-clicked", "yes");
                b(".myo-poll-holder-box-" + i).attr("data-myo-clicked", "yes")
            });
            b("#myo-poll-" + i + "-total").html("<br/><b>" + c.total + "</b> votes")
        });
        l.fail(function (d, c) {
            alert("Request failed: " + c)
        });
        return true
    })
});